﻿package dk.flashnotes.fff.contextMenu {
	import dk.flashnotes.fff.utils.browser.EBrowsers;	
	
	import dk.flashnotes.fff.utils.browser.BrowserUtils;	
	import dk.flashnotes.fff.FFFBase;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.ContextMenuEvent;
	import flash.events.EventDispatcher;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.system.System;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	import AS3;		

	/**
	 * @author jk
	 */
	public class ContextMenuNavigation extends EventDispatcher {
		private var _oContextLabelLanguage : ContextLabelLanguage;
		private var _oFFFBase : FFFBase;
		private var _oCM : ContextMenu;
		private var _sprSiteRoot : Sprite;
		private var _oCMIBack : ContextMenuItem;
		private var _oCMIForward : ContextMenuItem;
		private var _oCMCopyCurrentPageURL : ContextMenuItem;
		private var _oTreeLink : ITreeLink;
		private var _oCMIOpenLinkInNewWindow : ContextMenuItem;
		private var _oCMICopyLink : ContextMenuItem;
		private var _oCMIBrowser : ContextMenuItem;

		public function ContextMenuNavigation(oContextLabelLanguage : ContextLabelLanguage=null) {

			super();
			
			if (oContextLabelLanguage==null) {
				_oContextLabelLanguage = new ContextLabelLanguage();
			} else {
				_oContextLabelLanguage = oContextLabelLanguage;
			}
			
			setup();
		}
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// setup
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		private function setup() : void {
			
			_oFFFBase = FFFBase.get_oInstance();
			_oCM = new ContextMenu();
			
			_sprSiteRoot=get_sprSiteRoot();
			
			_oCM.hideBuiltInItems();
			
			//
			_oCMIBack = new ContextMenuItem(_oContextLabelLanguage.sBack);
			_oCMIBack.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onBack, false, 0, true);
			
			_oCMIForward = new ContextMenuItem(_oContextLabelLanguage.sForward);
			_oCMIForward.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onForward, false, 0, true);
			
			_oCMCopyCurrentPageURL = new ContextMenuItem(_oContextLabelLanguage.sCopyCuttentPageURL, true);
			_oCMCopyCurrentPageURL.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onCopyCurrentPageURLClicked, false, 0, true);
			
			_oCMIOpenLinkInNewWindow = new ContextMenuItem(_oContextLabelLanguage.sOpenLinkInNewWindow, true);
			_oCMIOpenLinkInNewWindow.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onOpenLinkInNewWindow, false, 0, true);
			
			_oCMICopyLink = new ContextMenuItem(_oContextLabelLanguage.sCopyLink);
			_oCMICopyLink.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onCopyLink, false, 0, true);
			
			//_oCM.customItems.push(_oCMIForward);

			if (BrowserUtils.sBrowser != EBrowsers.OPERA) {
				_oCM.customItems.push(_oCMIBack);
				_oCM.customItems.push(_oCMIForward);
			}
			_oCM.customItems.push(_oCMCopyCurrentPageURL);
			_oCM.customItems.push(_oCMIOpenLinkInNewWindow);
			_oCM.customItems.push(_oCMICopyLink);
			
			_oCM.addEventListener(ContextMenuEvent.MENU_SELECT, onMenuSelect, false, 0, true);
			_sprSiteRoot.contextMenu = _oCM;
			
			updateLinkItems();
		}
		

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// menu handlers
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function onForward(event : ContextMenuEvent) : void {
			_oFFFBase.oTreeControl.stepForward();
		}

		private function onBack(event : ContextMenuEvent) : void {
			_oFFFBase.oTreeControl.stepBack();
		}

		private function onCopyLink(event : ContextMenuEvent) : void {
			System.setClipboard(_oTreeLink.oTN.get_sFullUrl(false));
		}
		
		private function onOpenLinkInNewWindow(event : ContextMenuEvent) : void {
			navigateToURL(new URLRequest(_oTreeLink.oTN.get_sFullUrl(false)), "_blank");
		}

		private function onCopyCurrentPageURLClicked(event : ContextMenuEvent) : void {
			System.setClipboard(_oFFFBase.oTreeControl.oTNCurrent.get_sFullUrl());
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// open menu
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		private function onMenuSelect(event : ContextMenuEvent) : void {
			
			_oTreeLink=null;
			
			var doMousedItem : DisplayObject = event.mouseTarget;
			if (!doMousedItem || !event) {
				return;
			}
			
			while(doMousedItem!=_sprSiteRoot) {
				if(doMousedItem is ITreeLink) {
					if (ITreeLink(doMousedItem).oTN!=null) {
						_oTreeLink = (doMousedItem as ITreeLink);
						break;
					}
					updateLinkItems();
					break;
				}
				if (doMousedItem.parent) {
					doMousedItem = doMousedItem.parent;
				} else {
					_oTreeLink=null;
					break;
				}
			}
			updateLinkItems();
		}
		
		private function updateLinkItems() : void {
			
			if (_oTreeLink==null) {
				
				//_oCMIForward.visible=true;
				//_oCMIBack.visible=true;
				
				//_oCMCopyCurrentPageURL.visible=true;

				_oCMICopyLink.visible=false;
				_oCMIOpenLinkInNewWindow.visible=false;
				
			} else {

				//_oCMIForward.visible=false;
				//_oCMIBack.visible=false;
				
				//_oCMCopyCurrentPageURL.visible=false;

				_oCMICopyLink.visible=true;
				_oCMIOpenLinkInNewWindow.visible=true;
			}
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// find site root
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function get_sprSiteRoot() : Sprite {
			var doBase : DisplayObject = _oFFFBase.doPageContainer;
			while(doBase.parent!=doBase.stage) {
				doBase = doBase.parent;
			}
			return doBase as Sprite;
		}
	}
}
